package com.letoken.platform.job.jobhandler.monitor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.letoken.platform.job.redis.EmailSendRedis;
import com.letoken.platform.pub.util.EmailUtil;
import com.letoken.platform.pub.util.MD5Util;
import com.letoken.platform.repo.admin.dao.WithdrawMapper;
import com.letoken.platform.repo.admin.po.Withdraw;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author letokenlabs
 * @date 2023/3/2$
 * 提现需审核提醒
 */
@Component
public class WithdrawNotify {

    @Autowired
    WithdrawMapper withdrawMapper;

    @XxlJob("WithdrawNotify")
    public void withdrawNotify() {

        // xxl后台配置的参数
        // {"netType":"bsc","tokenId":"1","limitAmount":"1000","email":"xxx@gmail.com;xx1@gmail.com"}
        JSONObject params = JSON.parseObject(XxlJobHelper.getJobParam());
        String netType = params.getString("netType");
        Integer tokenId = params.getInteger("tokenId");
        BigDecimal limitAmount = params.getBigDecimal("limitAmount");
        String emailTo = params.getString("email");

        LambdaQueryWrapper<Withdraw> query = new LambdaQueryWrapper<>();
        query.eq(Withdraw::getTokenId,tokenId)
                .eq(Withdraw::getTransferMode, 1)
                .eq(Withdraw::getApprovedState, 0)
                .eq(Withdraw::getStatus, 0)
                .gt(Withdraw::getAmount, limitAmount);

        List<Withdraw> list = withdrawMapper.selectList(query);

        boolean alert = list != null && !list.isEmpty();

        if (alert) {

            String subject = String.format("链上提现申请，需人工审批！%s (%s)", netType, list.size());
            StringBuilder sb = new StringBuilder();
            sb.append("<div>链上提现申请 </div><ul>");

            list.forEach(it -> {
                sb.append(String.format("<li style='margin-bottom:5px'><div>编号: %s , 用户: %s, 币种: %s, 数量: %s </div><div>提现地址: %s, 申请时间: %s </div></li>",
                        it.getId(),
                        it.getUserId(),
                        getTokenName(it.getTokenId()),
                        it.getAmount(),
                        it.getAddress(),
                        it.getCreateTime()));
            });

            sb.append("</ul>");
            String key = MD5Util.MD5(sb.toString());
            if (!EmailSendRedis.me().isSending(key)) {
                EmailSendRedis.me().setSending(key, true, 30);
                boolean send = EmailUtil.sendEmail(emailTo, subject, sb.toString());
                if (!send) {
                    EmailSendRedis.me().setSending(key, false, 1);
                }
            }
        }
    }

    private String getTokenName(Integer tokenId) {
        return tokenId == 1 ? "USDT" : tokenId.toString();
    }
}
